
from PyQt4 import Qt
from PyQt4.QtGui import QItemDelegate, QComboBox
from PyQt4.QtCore import QString, SIGNAL

class SCTableDelegate(QItemDelegate):
    '''The base delegate for SCTableView.
    '''

    def __init__(self,parent=None):
        QItemDelegate.__init__(self, parent)

    def createEditor(self,parent,option,index):
        atom = index.model().getCell(index.row(),index.column())
        if index.model().isLocked(index):
            return
        if isinstance(atom.data,bool):
            editor = QComboBox(parent)
            editor.addItems(("true","false"))
            if atom.data:
                editor.setCurrentIndex(0)
            else:
                editor.setCurrentIndex(1)
            return editor
        return QItemDelegate.createEditor(self,parent,option,index)

    def setModelData(self, editor, model, index):
        if isinstance(editor, QComboBox):
            if str(editor.currentText().toLower()) == 'true':
                model.setData(index,True)
            else:
                model.setData(index,False)
        else:
            QItemDelegate.setModelData(self,editor,model,index)
